import 'package:boss/models/role.dart';
import 'package:boss/models/staff.dart';
import 'package:boss/ui/page/role/provider/role_provider.dart';
import 'package:boss/ui/page/staff/page/staffDetail/provider/staff_detail_provider.dart';
import 'package:boss/ui/widget/form/chip_form_field.dart';
import 'package:boss/ui/widget/column_divier.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/radioList/model/radio_item_data.dart';
import 'package:boss/ui/widget/radioList/radio_list_picker.dart';
import 'package:boss/ui/widget/title_row_button.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class StaffDetailForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: EdgeInsets.zero,
        child: Consumer<StaffDetailProvider>(
          builder: (context, staffDetailProvider, child) {
            return ColumnDivier(
              padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 20.w),
              children: <Widget>[
                Column(
                  children: <Widget>[
                    BaseTextFormField(
                      prefixText: '姓名',
                      isRequired: true,
                      hintText: '请输入',
                      initialValue: staffDetailProvider.staff.realname,
                      onChanged: (value) {
                        staffDetailProvider.staff.realname = value;
                      },
                    ),
                    ChipFormField<int>(
                      title: '性别',
                      items: [
                        ChipFormData(
                          title: '女',
                          value: Staff.GENDER_WOMAN,
                        ),
                        ChipFormData(
                          title: '男',
                          value: Staff.GENDER_MAN,
                        ),
                      ],
                      selectedItems: [staffDetailProvider.staff.gender],
                      onChanged: (items, item) {
                        staffDetailProvider.setStaffGender(item);
                      },
                    ),
                    BaseTextFormField(
                      prefixText: '手机（账号）',
                      isRequired: true,
                      hintText: '请输入',
                      maxLength: 11,
                      onChanged: (value) {
                        staffDetailProvider.staff.username = value;
                      },
                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                      keyboardType: TextInputType.number,
                      initialValue: staffDetailProvider.staff.username,
                    ),
                    ChipFormField<int>(
                      title: '是否全职',
                      items: [
                        ChipFormData(
                          title: '全职',
                          value: Staff.FULL_TIME_JOB,
                        ),
                        ChipFormData(
                          title: '兼职',
                          value: Staff.NOT_FULL_TIME_JOB,
                        )
                      ],
                      selectedItems: [staffDetailProvider.staff.isFulltimeJob],
                      onChanged: (items, item) {
                        staffDetailProvider.setStaffFullTimeJob(item);
                      },
                    ),
                    TitleRowButton(
                      title: '职位',
                      hintText: '请选择',
                      value: staffDetailProvider.staff.roleName,
                      isRequired: true,
                      onTap: () async {
                        if (staffDetailProvider.staff.superRole == 1) {
                          toast('${staffDetailProvider.staff.roleName}不能修改');
                          return;
                        }

                        var roleProvider = Provider.of<RoleProvider>(context, listen: false);

                        var find = roleProvider.roles.firstWhere((item) => item.roleId == staffDetailProvider.staff.roleId, orElse: () => null);

                        var items = roleProvider.roles.map((item) => RadioItemData(title: item.roleName, value: item)).toList();

                        var res = await showRadioListPicker<Role>(
                          context,
                          title: '职位',
                          value: [find],
                          isMultiple: false,
                          items: items,
                        );

                        if (res != null) {
                          staffDetailProvider.setStaffRole(res[0]);
                        }
                      },
                    ),
                  ],
                ),
                Column(
                  children: <Widget>[
                    BaseTextFormField(
                      prefixText: '最大折扣率',
                      hintText: '菜品或订单最大折扣，默认无折扣',
                      suffixText: '%',
                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                      maxLength: 2,
                      keyboardType: TextInputType.number,
                      initialValue: staffDetailProvider.staff.maxDiscount?.toString(),
                      onChanged: (value) {
                        staffDetailProvider.staff.maxDiscount = int.parse(value);
                      },
                    ),
                    BaseTextFormField(
                      prefixText: '减免金额上限',
                      hintText: '账单最大金额减免，不填无减免金额',
                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
                      keyboardType: TextInputType.numberWithOptions(decimal: true),
                      initialValue: AmountUtil.price2String(staffDetailProvider.staff.decreaseLimit),
                      onChanged: (value) {
                        staffDetailProvider.staff.decreaseLimit = AmountUtil.string2Price(value);
                      },
                    ),
                    BaseTextFormField(
                      prefixText: '身份证号',
                      hintText: '选填',
                      initialValue: staffDetailProvider.staff.idCard,
                      onChanged: (value) {
                        staffDetailProvider.staff.idCard = value;
                      },
                    ),
                  ],
                )
              ],
            );
          },
        ),
      ),
    );
  }
}
